/*
 * ============================================================================
 *
 *       Filename:  approc.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月08日 11时51分53秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#include "aclua.h"
#include "approc.h"

int ap_reg(struct apdef_t *apdef, char *apnetwork)
{
	assert(luahandle.L_main != NULL);
	lua_State *L = luahandle.L_main; 

	/* invoke lua func ap_reg */
	acctl_getglobal(L, "ap_reg");

	/* params 1 */
	lua_newtable(L);
	lua_pushinteger(L, apdef->isreg);
	lua_setfield(L, -2, "isreg");
	lua_pushinteger(L, apdef->ismine);
	lua_setfield(L, -2, "ismine");
	lua_pushstring(L, apdef->macstr);
	lua_setfield(L, -2, "macstr");
	lua_pushstring(L, apdef->acaddr);
	lua_setfield(L, -2, "acaddr");

	/* params 2 */
	lua_pushstring(L, apnetwork);

	acctl_pcall(L, 2, 1);

	if(!lua_isboolean(L, -1)) {
		sys_err("Lua function ap_reg should "
			"return a boolean\n");
		exit(-1);
	}

	int ret;
	ret = lua_toboolean(L, -1);
	lua_pop(L, 1);
	sys_debug("ap_reg: server adopt: %d\n", ret);
	return ret;
}
